public class IP_c22 {

    public static int IP_type(String ip) {
        // 初始化类型和标志
        int type = -1;
        boolean legal = false;
        boolean pri = false;

        // 检查IP地址是否合法
        String[] parts = ip.split("\\.");
        if (parts.length == 4) {
            try {
                int first = Integer.parseInt(parts[0]);
                int second = Integer.parseInt(parts[1]);
                int third = Integer.parseInt(parts[2]);
                int fourth = Integer.parseInt(parts[3]);

                // 检查每个部分是否在0到255之间
                if (first >= 0 && first <= 255 && second >= 0 && second <= 255 &&
                        third >= 0 && third <= 255 && fourth >= 0 && fourth <= 255) {
                    legal = true;

                    // 判断IP类别
                    if (first >= 1 && first <= 126) {
                        type = 0; // A类
                    } else if (first >= 128 && first <= 191) {
                        type = 1; // B类
                    } else if (first >= 192 && first <= 223) {
                        type = 2; // C类
                    } else if (first >= 224 && first <= 239) {
                        type = 3; // D类
                    } else if (first >= 240 && first <= 255) {
                        type = 4; // E类
                    }

                    // 判断是否为私有IP
                    if (first == 10 || (first == 172 && second >= 16 && second <= 31) ||
                            (first == 192 && second == 168)) {
                        pri = true;
                    }
                }
            } catch (NumberFormatException e) {
                // 如果解析失败，IP地址非法
                legal = false;
            }
        }

        if (!legal) {
            return -1;
        } else if (pri) {
            return type + 10;
        } else {
            return type;
        }
    }

}